<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

// Get query string variables.
// If in a check fighters state, we'll have a sector POST variable
if (isset($_GET['sector']))
	$sector = $_GET['sector'];
elseif (isset($_POST['sector']))
	$sector = $_POST['sector'];
else
{
	echo '
					<input id="status" type="hidden" value="error" />
					', bigtitle($txt['bnt_move_failed']);
	return;
}

$title = $txt['bnt_move_title'];

// Retrieve the user and ship information
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Check to see if the player has less than one turn available
// and if so return to the main menu
if ($playerinfo['turns'] < 1)
{
    echo '
					<input id="status" type="hidden" value="error" />
					', $txt['bnt_move_turn'];
	return;
}

// Retrieve all the sector information about the current sector
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_universe}
	WHERE sector_id = {int:sector_id}',
	array(
		'db_universe' => $dbtables['universe'],
		'sector_id' => $playerinfo['sector']
	)
);

// Put the sector information into the array "sectorinfo"
$sectorinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// Retrive all the warp links out of the current sector
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_links}
	WHERE link_start = {int:sector_id}',
	array(
		'db_links' => $dbtables['links'],
		'sector_id' => $playerinfo['sector']
	)
);

$i = 0;
$flag = 0;

//loop through the available warp links to make sure it's a valid move
while ($row = $smcFunc['db_fetch_assoc']($result))
{
	if ($row['link_dest'] == $sector && $row['link_start'] == $playerinfo['sector'])
	{
		$flag = 1;
	}
	$i++;
}
$smcFunc['db_free_result']($result);

//Check if there was a valid warp link to move to
if ($flag == 1)
{
	// These variables are used by check_fighters
    $ok = 1;
    $calledfrom = "move";
	
    include("check_fighters.php");
	
    if ($ok > 0)
	{
		$update = $smcFunc['db_query']('', '
			UPDATE {raw:db_ships}
			SET last_login = NOW(),
				turns = turns - 1,
				turns_used = turns_used + 1,
				sector = {int:sector}
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'sector' => $sector,
				'ship_id' => $playerinfo['ship_id']
			)
		);
		
		log_move($playerinfo['ship_id'],$sector);
		
		$result = $smcFunc['db_query']('', '
			SELECT turns, turns_used
			FROM {raw:db_ships}
			WHERE user_id = {int:user_id}
			LIMIT 1',
			array(
				'db_ships' => $dbtables['ships'],
				'user_id' => $user_info['id'],
			)
		);
		$moveresult = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);
		
		echo '
					<input id="turns_available" type="hidden" value="', NUMBER($moveresult['turns']), '" />
					<input id="turns_used" type="hidden" value="', NUMBER($moveresult['turns_used']), '" />';
	
		include("check_mines.php");
		
		if ($ok == 1)
			echo '
						<input id="status" type="hidden" value="complete" />';		
		else
			echo '
						<input id="status" type="hidden" value="mines" />';
    }
}
else
{
    echo '
					<input id="status" type="hidden" value="error" />
					', $txt['bnt_move_failed'];
	
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_ships}
		SET cleared_defences = NULL
		WHERE ship_id= {int:ship_id}',
		array(
			'db_ships' => $dbtables['ships'],
			'ship_id' => $playerinfo['ship_id']
		)
	);
}

?>
